//
//  ViewController.m
//  SavePhotos_Demo
//
//  Created by sp on 16/3/21.
//  Copyright © 2016年 sp. All rights reserved.
// 保存相册的 Demo 分享. 宋平. 2016.03

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, retain)UIImageView *imageForPhoto;

@end

@implementation ViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor grayColor];
    
    
    // 创建一个相片
    
    self.imageForPhoto = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Demo"]];
    self.imageForPhoto.frame = CGRectMake(0, 120, self.view.frame.size.width, self.view.frame.size.width*3/4);
    [self.view addSubview:self.imageForPhoto];
    
    // 打开图片响应者链
   self.imageForPhoto.userInteractionEnabled = YES;
    
    // 添加长按手势
    UILongPressGestureRecognizer *tapGesture = [[UILongPressGestureRecognizer alloc] init];
    [tapGesture addTarget:self action:@selector(tapSaveImageToIphone)];
    [ self.imageForPhoto addGestureRecognizer:tapGesture];

}



//保存方法

- (void)tapSaveImageToIphone{
    
    /**
     *  将图片保存到iPhone本地相册
     *  UIImage *image            图片对象
     *  id completionTarget       响应方法对象
     *  SEL completionSelector    方法
     *  void *contextInfo
     */
    
    
#pragma mark -- 获取响应者链
//    
//    id object = [self nextResponder];
//    
//    while (![object isKindOfClass:[SDPVCForWondelfulTrip class]]) {
//        object = [object nextResponder];
//    }
//    
//    SDPVCForWondelfulTrip *VC = (SDPVCForWondelfulTrip *)object;
//    
  
    
#pragma mark - 创建 Alert 提示窗
    
    // 准备初始化配置参数
    NSString *title = @"保存图片到本地";
    NSString *message = @"请查看手机相册";
    NSString *okButtonTitle = @"保存";
    NSString *laterButtonTitle = @"稍后保存";
    
    // 初始化
    UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    // 分别2个创建操作
    
    /** 确定保存*/
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
     
        
#pragma mark -- 核心代码  
        
        
        // 注解
        
        void UIImageWriteToSavedPhotosAlbum
        (
         UIImage  *image,
         id       completionTarget, // 完成目标
         SEL      completionSelector, // 完成的方法
         void     *contextInfo // 上下文信息 为 nil 空
         );
        
        /**
             *     UIImageWriteToSavedPhotosAlbum(drawImage.image, nil, nil, nil);
         
             *     id是target对象，sel是selector，即target对象上的方法名，contextInfo是任意指针，
                  会传递到selector定义的方法上。一般是当完成后调用方法时使用，或者在完成时出错的处理。
         
             *     completionSelector实现方法如下：
         
         - (void) image: (UIImage *) image  didFinishSavingWithError: (NSError *) error
         contextInfo: (void *) contextInfo;

         
         */
        
        UIImageWriteToSavedPhotosAlbum( self.imageForPhoto.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
        

        
        NSLog(@"保存成功");
    }];
    
    /** 稍后保存 */
    UIAlertAction *laterAction = [UIAlertAction actionWithTitle:laterButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        NSLog(@"稍后保存");
        
    }];
    
    
    
    // 添加操作（顺序就是呈现的上下顺序）
    [alertDialog addAction:laterAction];
    [alertDialog addAction:okAction];
    
    // 呈现警告视图
    [self presentViewController:alertDialog animated:YES completion:nil];
    
    
    
}


#pragma mark -- 保存图片方法



// 协议方法

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = @"保存";
    if (!error) {
        
        UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:@"保存成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertDialog animated:YES completion:nil];
        
        
#pragma mark -- 设定计时器,进行时间设定,自动消失
        [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(creatAlert:) userInfo:alertDialog repeats:NO];

        
    }else
    {
        message = [error description];
    }
    NSLog(@"message is %@",message);
    
}


// 计时器方法

- (void)creatAlert:(NSTimer *)timer{
    UIAlertController *alert = [timer userInfo];
    [alert dismissViewControllerAnimated:YES completion:nil];
    alert = nil;
    
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
